
package physical_spaces.beans;

import Entity.DataFile;
import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;
import physical_spaces.business.DataFileDataModel;
import physical_spaces.business.PhysicalSpaceBusiness;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean
@SessionScoped
public class AdministracionDataFileBean  implements Serializable{
    
    private List<DataFile> dataFields ;
    private static final long serialVersionUID = 408L;
    DataFileDataModel mediumDataFileModel;
    public DataFile selectedDataFile;
    private String nameDataFile;
    private String dataFileSizeOption;
    private String actualSize;
    private String newExtendedSize;
    
    //propiedades componentes
    private int index;
    private String disabled;
    private String disabledParent;
    private Connection conn;

    public AdministracionDataFileBean() {
        
        index = 0;
        disabled = "true";
        
        dataFileSizeOption = "K";
     
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");
        
        conn = conectionBean.currentConnection;
        
        dataFields = new ArrayList<>();
        chargeData();
        mediumDataFileModel = new DataFileDataModel(dataFields);

    }

    public DataFileDataModel getMediumDataFileModel() {
        return mediumDataFileModel;
    }

    public void setMediumDataFileModel(DataFileDataModel mediumDataFileModel) {
        this.mediumDataFileModel = mediumDataFileModel;
    }

    public DataFile getSelectedDataFile() {
        return selectedDataFile;
    }

    public void setSelectedDataFile(DataFile selectedDataFile) {
        this.selectedDataFile = selectedDataFile;
    }

    public DataFileDataModel getMediumTableSpaceModel() {
        return mediumDataFileModel;
    }   

    public String getNameDataFile() {
        return nameDataFile;
    }

    public void setNameDataFile(String nameDataFile) {
        this.nameDataFile = nameDataFile;
    }
    
    public List<DataFile> getDataFields() {
        return dataFields;
    }

    public void setDataFields(List<DataFile> dataFields) {
        this.dataFields = dataFields;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getActualSize() {
        return actualSize;
    }

    public void setActualSize(String actualSize) {
        this.actualSize = actualSize;
    }

    public String getDisabled() {
        return disabled;
    }

    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }

    public String getDisabledParent() {
        return disabledParent;
    }

    public void setDisabledParent(String disabledParent) {
        this.disabledParent = disabledParent;
    }

    public String getDataFileSizeOption() {
        return dataFileSizeOption;
    }

    public void setDataFileSizeOption(String dataFileSizeOption) {
        this.dataFileSizeOption = dataFileSizeOption;
    }

    public String getNewExtendedSize() {
        return newExtendedSize;
    }

    public void setNewExtendedSize(String newExtendedSize) {
        this.newExtendedSize = newExtendedSize;
    }
    
    
    public void cancel(){
        chargeData();
        mediumDataFileModel = new DataFileDataModel(dataFields);
        index = 0;
        disabledParent = "false";
        disabled = "true";
    }
    
    public void changeTab(){
        FacesContext context = FacesContext.getCurrentInstance();  
        if(selectedDataFile == null)
            context.addMessage(null,new FacesMessage("Error ","Por favor seleccione un DataFile"));
        else{
            nameDataFile = selectedDataFile.getName();
            actualSize = selectedDataFile.getTamano();
            actualSize = actualSize + " Bytes";
            newExtendedSize = "";
            dataFileSizeOption = "K";
            disabledParent = "true";
            disabled = "false";
            index++;
        }    
    }
    
    public void deleteDataFile() {
        
        FacesContext context = FacesContext.getCurrentInstance();
        PhysicalSpaceBusiness physicalSpaceBusiness = new PhysicalSpaceBusiness(conn);
        if (selectedDataFile == null) {
            context.addMessage(null, new FacesMessage("Error ", "Por favor seleccione un DataFile"));
        } else {
            context.addMessage(null,new FacesMessage("Mensaje ",physicalSpaceBusiness.crearQueryEliminarDataFile(selectedDataFile.getTableSpace(),selectedDataFile.getName())));
            chargeData();
            mediumDataFileModel = new DataFileDataModel(dataFields);
        }
    }
    
    public void expandirTamano() {
    
        FacesContext context = FacesContext.getCurrentInstance();
        PhysicalSpaceBusiness physicalSpaceBusiness 
                = new PhysicalSpaceBusiness( conn );
        if (newExtendedSize.equals("")) {
            context.addMessage(null, new FacesMessage("Error ", "Por favor ingrese un tamaño"));
        } else {
            context.addMessage(null,new FacesMessage("Mensaje ",physicalSpaceBusiness.reSizeDataFile(nameDataFile,newExtendedSize,dataFileSizeOption)));
            chargeData();
            mediumDataFileModel = new DataFileDataModel(dataFields);
            for(DataFile data : mediumDataFileModel){
                if(data.getName().equals(nameDataFile)){
                    actualSize = data.getTamano();
                    actualSize = actualSize + " Bytes";
                }
            }
        }
        
    }
    

    private void chargeData() {
        PhysicalSpaceBusiness psb = new PhysicalSpaceBusiness( conn );
        dataFields = psb.selectDataFiles();
    }
    
    
    
}